package com.android.canvasproject.view.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.canvasproject.R;
import com.android.canvasproject.enums.CanvasTypeEnum;
import com.android.canvasproject.model.entity.Postit;
import com.android.canvasproject.model.listeners.BusinessModelCanvasListener;
import com.android.canvasproject.model.listeners.PostitInternal;
import com.android.canvasproject.model.listeners.ProjectModelCanvasListener;

public class ComponentViewUtil {
	
	private ComponentViewUtil(){}
	
	public static TextView getTextView (Context contexto, String text, int style, int ... paddind) {
		TextView txt = null;

		if(contexto != null){
			txt = new TextView(contexto);
			txt.setText(text);
			if(style > 0)
				txt.setTextAppearance(contexto, style);

			if(paddind.length == 4)
				txt.setPadding(paddind[0], paddind[1], paddind[2], paddind[3]);
		}
		return txt;
	}
	
	public static LinearLayout getLinearLayout(Context contexto, int orientation) {
		LinearLayout layout = null;

		if(contexto != null){
			layout = new LinearLayout(contexto);
			layout.setOrientation(orientation);
		}
		return layout;
	}
	
	public static ImageButton getImageButton(Context contexto, int imageResource, int backgroundColor, int id, int width){
		ImageButton image = null;
		Bitmap bMap = null;
		
		if(contexto != null){
			image = new ImageButton(contexto);
			image.setBackgroundColor(backgroundColor);

			if(id > 0)
				image.setId(id);

			if(imageResource > 0){
				bMap = BitmapFactory.decodeResource(contexto.getResources(), imageResource);
				image.setImageDrawable(resizeDrawable(contexto, imageResource, width, getProportionalHeight(width, bMap.getWidth(), bMap.getHeight())));
			}
		}
		return image;
	}
	
	public static ImageButton getImageButton(Context contexto, int imageResource, int id){
		ImageButton image = null;

		if(contexto != null){
			image = new ImageButton(contexto);

			if(id > 0)
				image.setId(id);

			if(imageResource > 0)
				image.setBackgroundResource(R.drawable.selector_canvas_project);
		}
		return image;
	}
	
	public static ImageButton getImageButton(Context contexto, int imageResource, int backgroundColor, int id){
		ImageButton image = null;

		if(contexto != null){
			image = new ImageButton(contexto);
			image.setBackgroundColor(backgroundColor);

			if(id > 0)
				image.setId(id);

			if(imageResource > 0)
				image.setImageResource(imageResource);
		}
		return image;
	}
	
	public static Drawable resizeDrawableForScreen(Context ctx, int image) {
		int deviceWidth = ctx.getResources().getDisplayMetrics().widthPixels;

		BitmapDrawable bitMap = (BitmapDrawable) ctx.getResources().getDrawable(image);
		double imgWidth = bitMap.getBitmap().getWidth();
		double imgHeight = bitMap.getBitmap().getHeight();

		double ratio = deviceWidth / imgWidth;
		int newImageHeight = (int) (imgHeight * ratio);

		Bitmap bMap = BitmapFactory.decodeResource(ctx.getResources(), image);
		Drawable drawable = new BitmapDrawable(ctx.getResources(), resizedBitmap(bMap,newImageHeight,(int) deviceWidth));

		return drawable;
	}

	private static Bitmap resizedBitmap(Bitmap bMap, int newHeight, int newWidth) {
		float scaleWidth = ((float) newWidth) / bMap.getWidth();
		float scaleHeight = ((float) newHeight) / bMap.getHeight();

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);

		Bitmap resizedBitmap = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, false);
		return resizedBitmap;
	}
	
	public static Drawable resizeDrawable(Context ctx, int imageDrawable, int width, int height) {
		Bitmap bMap = BitmapFactory.decodeResource(ctx.getResources(), imageDrawable);
		Drawable drawable = new BitmapDrawable(ctx.getResources(), resizedBitmap(bMap, height, width));
		
		return drawable;
	}
	
	public static int getProportionalHeight(int newWidth, int oldWidth, int oldHeight) {
		return (int) (newWidth * oldHeight)/oldWidth;
	}
	
	public static Bitmap getBitmapForImageResource(Context ctx, int imageResource){
		return BitmapFactory.decodeResource(ctx.getResources(), imageResource);
	}
	
	public static LinearLayout createNewCanvas(Activity activity, String title, String detail, CanvasTypeEnum canvasType, Object tag){
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View viewContext =  inflater.inflate(R.layout.new_canvas, null, false);
		
		//CREATING A COMPONENT [IMAGEBUTTON]
		ImageButton projectButton = (ImageButton) viewContext.findViewById(R.id.imagebutton_canvas);
		if(canvasType.equals(CanvasTypeEnum.PROJECT_MODEL_CANVAS)) {
			projectButton.setBackgroundResource(R.drawable.selector_canvas_project);
			projectButton.setOnClickListener(new ProjectModelCanvasListener());
			projectButton.setOnLongClickListener(new ProjectModelCanvasListener());

		} else if(canvasType.equals(CanvasTypeEnum.BUSINESS_MODEL_CANVAS)){
			projectButton.setBackgroundResource(R.drawable.bmc_resize);
			projectButton.setOnClickListener(new BusinessModelCanvasListener());
			projectButton.setOnLongClickListener(new BusinessModelCanvasListener());

		} else if(canvasType.equals(CanvasTypeEnum.ANALISYS_SWOT)){
			Toast.makeText(activity, "[ANALISYS SWOT] Projeto não implementado!", Toast.LENGTH_LONG).show();
			
		} else if(canvasType.equals(CanvasTypeEnum.KANBAN)){
			Toast.makeText(activity, "[KANDAN] Projeto não implementado!", Toast.LENGTH_LONG).show();
			
		}

		//CREATING A COMPONENT [TEXTVIEW]
		TextView labelProject = (TextView) viewContext.findViewById(R.id.textview_project_title);
		labelProject.setText(title);
		projectButton.setTag(tag);

		//CREATING A COMPONENT [LINEARLAYOUT] AND DEFINING ANIMATION TO CREATE CANVAS
		Animation anime = AnimationUtils.loadAnimation(activity, R.anim.zoom_in);
		ConstantsAction.Animation.START_OFF_SET += 30;
		anime.setStartOffset(ConstantsAction.Animation.START_OFF_SET);
		
		LinearLayout layoutCanvas = (LinearLayout) viewContext.findViewById(R.id.layout_canvas);
		layoutCanvas.startAnimation(anime);

		return layoutCanvas;
	}
	
	public static void createNewPostit(Activity activity, float positionX, float positionY, RelativeLayout layoutCanvas, Postit p) {
		Context context = layoutCanvas.getContext();
		LayoutInflater inflater = (LayoutInflater) ((Activity) context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		RelativeLayout postit = (RelativeLayout) inflater.inflate(R.layout.new_postit, null, false);
		postit.setLayoutParams(new LayoutParams(130, 130));
		postit.setBackgroundResource(R.drawable.background_layout_postit_yellow);
		postit.setX(positionX);
		postit.setY(positionY);
		postit.setTag(p.getId());

		switch (p.getColor()) {
			case COLOR_BLUE:
				postit.setBackgroundResource(R.drawable.background_layout_postit_blue);
				break;

			case COLOR_GREEN:
				postit.setBackgroundResource(R.drawable.background_layout_postit_green);
				break;

			case COLOR_RED:
				postit.setBackgroundResource(R.drawable.background_layout_postit_red);
				break;

			case COLOR_YELLOW:
				postit.setBackgroundResource(R.drawable.background_layout_postit_yellow);
				break;

			default:
				postit.setBackgroundResource(R.drawable.background_layout_postit_yellow);
				break;
		}

		Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/angelina.ttf");
		TextView titlePostit = (TextView) postit.findViewById(R.id.title_postit);
		titlePostit.setText(p.getTitle());
		titlePostit.setTypeface(font);
		titlePostit.setTextSize(23f);

		postit.setOnClickListener(new PostitInternal(activity));
		postit.setOnTouchListener(new PostitInternal(activity));

		layoutCanvas.addView(postit);
	}
	
	public static View createNewPostit(Activity activity, float positionX, float positionY, Postit p) {
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		RelativeLayout postit = (RelativeLayout) inflater.inflate(R.layout.new_postit, null, false);
		postit.setLayoutParams(new LayoutParams(130, 130));
		postit.setBackgroundResource(R.drawable.background_layout_postit_yellow);
		postit.setX(positionX);
		postit.setY(positionY);
		postit.setTag(p.getId());

		switch (p.getColor()) {
			case COLOR_BLUE:
				postit.setBackgroundResource(R.drawable.background_layout_postit_blue);
				break;

			case COLOR_GREEN:
				postit.setBackgroundResource(R.drawable.background_layout_postit_green);
				break;

			case COLOR_RED:
				postit.setBackgroundResource(R.drawable.background_layout_postit_red);
				break;

			case COLOR_YELLOW:
				postit.setBackgroundResource(R.drawable.background_layout_postit_yellow);
				break;

			default:
				postit.setBackgroundResource(R.drawable.background_layout_postit_yellow);
				break;
		}

		Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/angelina.ttf");
		TextView titlePostit = (TextView) postit.findViewById(R.id.title_postit);
		titlePostit.setText(p.getTitle());
		titlePostit.setTypeface(font);
		titlePostit.setTextSize(23f);

		postit.setOnClickListener(new PostitInternal(activity));
		postit.setOnTouchListener(new PostitInternal(activity));
		
		return postit;
	}
}